package second;

import Test.Test;

import java.util.Scanner;

public class Ostest {
    public static void main(String[] args) {
        int[] datas = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1};
        //分配主存的块数
        Scanner input = new Scanner(System.in);
        System.out.print("请输入分配主存的块数:");
        int length = input.nextInt();//length表示主存的块数
        int[] memory = new int[length];
        for (int i = 0; i < memory.length; i++)
            memory[i] = -1;//将数组中的值设为-1
        //DataShow(memory, datas);
        int[] a={2,4,8};
    }

    public static void ChangeOfMemory_LRU1(int[] memory, int data, int[] Count) {
        int length = memory.length;//数组memory的长度
        for (int i = 0; i < length; i++) {
            if (memory[i] >= 0)
                Count[i]++;
        }
        int size = get_size(memory, data, Count);
        memory[size] = data;
        Count[size] = 0;
    }

    public static int get_size(int[] memory, int data, int[] Count) {
        int length = memory.length;//数组memory的长度
        for (int i = 0; i < length; i++)
            if (memory[i] < 0)
                return i;
        int size = 0;
        int num = memory[0];
        if (ls_connected(memory, data)) {
            for (int i = 0; i < length; i++) {
                if (Count[i] > num) {
                    num = Count[i];
                    size = i;
                }
            }
            return size;
        } else {
            for (int i = 0; i < length; i++)
                if (memory[i] == data)
                    return i;
        }
        return 0;
    }

    public static boolean ls_connected(int[] memory, int data) {
        int length = memory.length;//数组memory的长度
        for (int i = 0; i < length; i++)
            if (memory[i] == data)
                return false;
        return true;
    }

    public static void DataShow(int[] memory, int[] datas) {
        System.out.println("数据变化情况:");
        int length = memory.length;//数组memory的长度
        int length_sum = datas.length;//数组datas的长度,也是页面的总数
        int count = 0;//记录缺页的数量
        int[] Count = new int[length];//计数器
        for (int i = 0; i < length; i++)
            Count[i] = 0;
        int k = 0;//记录替换指针的值
        for (int i = 0; i < length_sum; i++) {
            if (ls_connected(memory, datas[i])) {
                count++;
                ChangeOfMemory_LRU1(memory, datas[i], Count);
                for (int j = 0; j < length; j++) {
                    if (memory[j] >= 0)
                        System.out.print(memory[j] + " ");
                    else
                        System.out.print("  ");
                }
                System.out.println();
                for (int j = 0; j < length; j++)
                    System.out.print(Count[j] + "-");
                System.out.print("×");
                System.out.println();
            } else {


                ChangeOfMemory_LRU1(memory, datas[i], Count);
                for (int j = 0; j < length; j++)
                    System.out.print(memory[j] + " ");
                System.out.println();
                for (int j = 0; j < length; j++)
                    System.out.print(Count[j] + "-");

                System.out.print("√");
                System.out.println();
            }
        }

        System.out.println("算法结束,缺页率为:" + (double) count / length_sum);
    }
}
